package wili.attrMapping._user.b1;

import java.util.function.BiFunction;
import java.util.regex.Matcher;

import wili.attrMapping.core.Att;
import wili.attrMapping.core.Att9;
import wili.attrMapping.core.Atts;
import wili.attrMapping.core.AttsDefine;
import wili.c.Ca;
import wili.c.Str9a;

/**
 * 
 * @author weilai2@163.com
 *
 */
public class XmlSalesDefine extends AttsDefine {

	public String approver;
	public String productType;
	public String salesType;
	public String inventory;
	public String salesQuantity;
	//public String editable;

	public XmlSalesDefine(boolean isNew, String keyName, String objName, String targetAttName) {
		super(isNew, keyName, objName, targetAttName);
	}

	@Override
	public String valueKindOfAtt(String att, String value) {
		if (att.equals("inventory")) {
			return "@{inventory}";
		}
		if (att.equals("salesQuantity")) {
			return "@{salesQuantity}";
		}
		return value;
	}

	public String valueOfNewAtt(String ruleValue, Atts oldAtts) {
		//规则、旧属性集 ~>新属性值。
		Ca.asert(false, "todo12-12");
		Att9.ref("新属性=fun(规则，旧属性)");
		Ca.log(-1, updateID, ", oldAtts.key=", oldAtts.key);
		String toValue = ruleValue;
		{// updateValue时，【把规则值中的占位符 ~>属性值~>新属性值】
			toValue = Str9a.replaceRegMatchWithFun("@\\{(\\w+)\\}", toValue, new BiFunction<String, Matcher, String>() {
				@Override
				public String apply(String s0, Matcher match) {

					String varName = match.group(1);
					Att oldAtt = oldAtts.att(varName);
					if (varName.equals("formula")) {
						String oldFm = oldAtt.value;
						oldFm = B1com.formularTransform(oldAtts, oldFm, false);
						oldFm = B1com.quoteNum_relatesToObjType(oldFm);
						return oldFm;
					} else {
						Ca.asert(false, "ok");
						return oldAtt.value;
					}
				}
			});
		}
		{//进一步处理规则值 ~>新属性值
			++updateID;
			toValue = Str9a.removeRedundantBracket('(', ')', toValue);
			toValue = B1com.closeFormula(toValue);
			oldAtts.updateNewAtts(toValue);
			// Ca.log(updateID + ", RuleID=" + ruleLeaf.id + ", newValue=" + toValue);
			oldAtts.file.getUpdateTimes()[0]++;
		}
		return toValue;
	}

	static int updateID;
}
